PHP Switch Statement

Switch is another conditional statement execute depending on each other condition.Syntex of switch satement is given below--  

Syntex of Switch.....statement

switch(variable)
{
case conditionvalue :
execute if case codition is trure ;
break;
case conditionvalue2 :
execute if case codition is trure ;
break;
case conditionvalue3 :
execute if case codition is trure ;
break;
... defoalt :
Otherwise it will be executed ;
}
  We have given example of this condition below within one page using php and html. This is just like a mini calculation project. Where we can do add, subtraction, multiply and division

Example

<?php if(isset($_POST['done'])) { $n1=$_POST['t1']; $n2=$_POST['t2']; $ch=$_POST['choice']; switch($ch) { case 'add': $res=$n1+$n2; break; case 'sub': $res=$n1-$n2; break; case 'mul': $res=$n1*$n2; break; case 'div': $res=$n1/$n2; break; default: $res="You didn't select radio button"; } ?> <div style='position:absolute;left:0px;top:0pc;width:200px;heght:100px;'> Enter First Number<input type="text" name="t1"><br> Enter Second Number<input type="text" name="t2"><br> Result<input type="text" value="<?php echo $res;?>"> Select Task: <input type="radio" name="choice" value="add">ADD <input type="radio" name="choice" value="sub">SUB <input type="radio" name="choice" value="mul">MUL <input type="radio" name="choice" value="div">DIV<br> <input type="submit" name="done" value="DONE"> </div> <?php } ?> Enter First Number<input type="text" name="t1"><br> Enter Second Number<input type="text" name="t2"><br> Result<input type="text" value=""> Select Task: <input type="radio" name="choice" value="add">ADD <input type="radio" name="choice" value="sub">SUB <input type="radio" name="choice" value="mul">MUL <input type="radio" name="choice" value="div">DIV<br> <input type="submit" name="done" value="DONE">



All Tutorial => 12345678910





Write Comment